var topHtml = null;			//用户首页初始状态HTML
var selectedDate;			//当前选中日期
var selectedTab;			//当前选中标签（"customer_maintain", "request_list", "message", "merchant_info"）
var customers = null;		//用户查找结果
var customerListHtml;		//用户查找结果HTML
var selectedCusutomerIndex;	//当前选中用户序号
var histories;				//选中用户的养护列表
var historyListHtml;		//养护列表HTML
var requestListHtml;		//请求列表HTML
var customerDetailReferer;
var orderDetailReferer;
var selectedRequestId;	//当前选中请求Id
var selectedMaintainOrderId;	//当前选中养护记录Id
var selectedMaintainOrderLicense;	//当前选中养护车牌
var htmlBeforeOrderDetail;	//进入养护记录详情前的HTML
var htmlBeforeChangePwd;	//进入修改密码前的HTML

function selectMaintainItem(element) {
	$(".order_item div").css("background", "#d7d2c4");
	$(element).children("div").css("background", "#6b3344");
}

function show_add_customer_tab() {	
	$.post("add_customer.jsp", "", function(ret) {
		$("#div_top_tabhost").html(ret);
	});
}

function show_search_customer_tab() {	
	$.post("search_customer.jsp", "", function(ret) {
		$("#div_top_tabhost").html(ret);
	});
}

function show_add_maintain_order_tab() {
	$.post("add_maintain_order.jsp", "", function(ret) {
		$("#div_top_tabhost").html(ret);
	});
}

function showCustomerTab() {
	$.post("maintain_customer.jsp", "", function(ret){
		clearData();
		selectedTab = "customer_maintain";
		
		$("#div_top_tabhost").html(ret);
	});
}

function showRequestTab(remainStatus) {
	if (!remainStatus) {
		selectedRequestId = "";
	}
	
	$.post("request_list.jsp", "", function(ret){
		clearData();
		selectedTab = "request_list";
		
		$("#div_top_tabhost").html(ret);
	});
}

function showMessageTab() {
	$.post("message_list.jsp", "", function(ret){
		clearData();
		selectedTab = "message";
		
		$("#div_top_tabhost").html(ret);
	});
}

function showMerchantInfoTab() {
	$.post("merchant_basic_info.jsp", "", function(ret){
		clearData();
		selectedTab = "merchant_info";
		
		$("#div_top_tabhost").html(ret);
	});
}

function showPlaceList() {
	$.post("place_manage.jsp", "", function(ret){		
		$("#div_top_tabhost").html(ret);
	});
}

function showPlaceRequests() {
	$.post("place_request_manage.jsp", "", function(ret){		
		$("#div_top_tabhost").html(ret);
	});
}

function showInitpage() {
	if (cover != null) {
		$(cover).remove();
	}
	$("#div_top_tabhost").html(topHtml);
	$("#datePicker").val(selectedDate);
}

function showUserInfo(index, referer) {
	selectedCusutomerIndex = index;
	customerDetailReferer = referer;
	
	$.post("customer_detail.jsp", "", function(ret) {
		$("#div_top_tabhost").html(ret);
		
		addFadeEffect();
		
		if (customers != null) {
			$("#license").val(customers[index].carLicense);
			$("#model").val(customers[index].carModel);
			$("#displacement").val(customers[index].carDisplacement);
			$("#buy_date").val(customers[index].buyDate);
			$("#img_merchant").attr("src", WSContextPath + "/" +customers[index].carPhoto);
			$("#name").val(customers[index].name);
			$("#mobile").val(customers[index].mobile);
			
			//从客户列表页进来时，返回客户列表
			$("#btn_return").click(showCustomerList);
		} else {
			var requestUrl = contextPath + "/merchant/search_customer";
			var params = {license_number:selectedMaintainOrderLicense};
			
			//根据当前记录的车牌查询客户信息
			$.post(requestUrl, params, function(data){
				try {
					var item = data.resultData.items[0];
					
					$("#license").val(item.carLicense);
					$("#model").val(item.carModel);
					$("#displacement").val(item.carDisplacement);
					$("#buy_date").val(item.buyDate);
					$("#img_merchant").attr("src", WSContextPath + "/" + item.carPhoto);
					$("#name").val(item.name);
					$("#mobile").val(item.mobile);
				} catch (e) {
				}
			}, "json");
			
			//从请求列表页进来时，返回请求列表。 其他情况隐藏返回按扭
			if ("request_list" == selectedTab) {
				$("#btn_return").click(function(){showRequestTab(true);});
			} else {
				$("#btn_return").css("display", "none");
			}
		}
	});
}

//进入养护记录详情页
function showMaintainOrderDetail(orderId, referer) {
	orderDetailReferer = referer;
	selectedMaintainOrderId = orderId;
	htmlBeforeOrderDetail = $("#div_top_tabhost").html();
	
	$.post("maintain_order_detail.jsp", "", function(ret) {
		$("#div_top_tabhost").html(ret);
	});
}

//跳出养护记录详情页
function exitMaintainOrderDetail() {
	$("#div_top_tabhost").html(htmlBeforeOrderDetail);
	addFadeEffect();
}

function showMaintainList() {	
	$.post("maintain_history_list.jsp", "", function(ret) {
		$("#div_top_tabhost").html(ret);
	});
}

//跳转到添加养护记录页
function showAddMaintainOrder() {
	historyListHtml = $("#div_top_tabhost").html();
		
	$.post("add_maintain_order.jsp", "", function(ret) {
		$("#div_top_tabhost").html(ret);
	});
}

//跳转到养护记录列表页
function showMaintainHistory() {
	$("#div_top_tabhost").html(historyListHtml);
	addFadeEffect();
}

//跳转到客户列表页
function showCustomerList() {
	$("#div_top_tabhost").html(customerListHtml);
	addFadeEffect();
}

//跳转到修改密码页
function showChangePwd(){
	htmlBeforeChangePwd = $("#div_top_tabhost").html();
	
	$.post("change_pwd.jsp", "", function(ret) {
		$("#div_top_tabhost").html(ret);
	});
}

//跳出修改密码页
function exitChangePwd(){
	$("#div_top_tabhost").html(htmlBeforeChangePwd);
}

function clearData() {
	customers = null;
}